﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CardMonitor.Windows
{
    /// <summary>
    /// PlateKeyboardWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PlateKeyboardWindow : Window
    {
     
            public event EventHandler<string> PlateTextChanged;
            public event EventHandler Confirmed;
            private string _plateText = "";
            public string PlateText
            {
                get => _plateText;
                private set
                {
                    _plateText = value;
                    PlateTextChanged?.Invoke(this, _plateText);
                }
            }

            public PlateKeyboardWindow()
            {
                InitializeComponent();
                // 处理所有按钮的点击事件（除了已经单独处理的功能键）
                foreach (var button in FindVisualChildren<Button>(this))
                {
                    // 跳过已经单独处理的功能键
                    if (button.Content.ToString() == "←" ||
                        button.Content.ToString() == "清空" ||
                        button.Content.ToString() == "确认" ||
                        button.Content.ToString() == "✕")
                        continue;

                    button.Click += KeyButton_Click;
                }
            }

            private void KeyButton_Click(object sender, RoutedEventArgs e)
            {
                if (sender is Button button && button.Content != null)
                {
                    // 限制车牌号长度（最多8位）
                    if (PlateText.Length < 8)
                    {
                        PlateText += button.Content.ToString();
                    }
                }
            }

            private void DeleteButton_Click(object sender, RoutedEventArgs e)
            {
                if (PlateText.Length > 0)
                {
                    PlateText = PlateText.Substring(0, PlateText.Length - 1);
                }
            }

            private void ClearButton_Click(object sender, RoutedEventArgs e)
            {
                PlateText = "";
            }

            private void ConfirmButton_Click(object sender, RoutedEventArgs e)
            {
                Confirmed?.Invoke(this, EventArgs.Empty);
                Close();
            }

            private void CloseButton_Click(object sender, RoutedEventArgs e)
            {
                Close();
            }

            // 辅助方法：遍历所有子控件
            private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj == null) yield break;

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    var child = VisualTreeHelper.GetChild(depObj, i);
                    if (child is T t)
                    {
                        yield return t;
                    }

                    foreach (var childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

       
    
}
